package com.boang.controller.parking;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boang.entity.PO.Parkingspot;
import com.boang.resp.Result;
import com.boang.service.ParkingspotService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
@author:22567
@date:2025/3/10 19:17
*/
@Tag(name = "停车位管理", description = "停车位相关接口")
@RestController
@RequestMapping("/parkingspot")
public class ParkingspotController {
    @Autowired
    private ParkingspotService parkingspotService;

    //-------------------添加--------------------
    @Operation(summary = "添加停车场")
    @PostMapping("/addspot")
    public Result<Boolean> add(@RequestBody Parkingspot parkingspot) {
        return Result.ok(parkingspotService.save(parkingspot));
    }

    //-------------------修改--------------------
    @Operation(summary = "修改场地")
    @PutMapping("/updatespot")
    public Result<Boolean> update(@RequestBody Parkingspot parkingspot) {
        return Result.ok(parkingspotService.updateById(parkingspot));
    }

    //-------------------删除--------------------
    @Operation(summary = "删除场地")
    @DeleteMapping("/removespot")
    public Result<Boolean> remove(int id) {
        return Result.ok(parkingspotService.removeById(id));
    }

    //-------------------查询--------------------

    @Operation(summary = "条件查询停车场")
    @PostMapping("/page")
    public IPage<Parkingspot> page(@RequestBody Parkingspot parkingspot) {
        QueryWrapper<Parkingspot> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.lambda()
                .like(parkingspot.getParkingLotName() != null, Parkingspot::getParkingLotName, parkingspot.getParkingLotName())
                .eq(parkingspot.getSpotNumber() != null, Parkingspot::getSpotNumber, parkingspot.getSpotNumber())
                .eq(parkingspot.getSpotStatus() != null, Parkingspot::getSpotStatus, parkingspot.getSpotStatus());
        if (parkingspot.getPageNum() < 1) parkingspot.setPageNum(1);
        if (parkingspot.getPageSize() < 1 || parkingspot.getPageSize() > 100) parkingspot.setPageSize(10);
        Page<Parkingspot> page = new Page<>(parkingspot.getPageNum(), parkingspot.getPageSize());
        return parkingspotService.page(page, QueryWrapper);

    }

}
